package frame;

import entity.Book;
import entity.Category;
import service.BookServiceImpl;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class AddBook extends JFrame {

    JLabel l_bid, l_name, l_author, l_price, l_number, l_firstlevel, l_secondlevel;
    JTextField j_bid, j_name, j_author, j_price, j_number, j_firstlevel, j_secondlevel;
    JButton b_add,b_reset;

    public AddBook() {
        this.setTitle("����Ա����鼮����");
        this.setSize(350, 450);
        this.setLocation(250, 150);
        init();
        this.setVisible(true);
    }

    public void init() {
        l_bid = new JLabel("�鼮���", JLabel.LEFT);
        l_name = new JLabel("�鼮����", JLabel.RIGHT);
        l_author = new JLabel("����", JLabel.LEFT);
        l_price = new JLabel("����", JLabel.LEFT);
        l_number = new JLabel("�����");
        l_firstlevel = new JLabel("��һ������");
        l_secondlevel = new JLabel("�ڶ�������");

        j_bid = new JTextField();
        j_name = new JTextField();
        j_author = new JTextField();
        j_price = new JTextField();
        j_number = new JTextField();
        j_firstlevel = new JTextField();
        j_secondlevel = new JTextField();

        b_add = new JButton("ȷ�����");
        b_add.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                // TODO Auto-generated method stub
                String bid = j_bid.getText();
                String name = j_name.getText();
                String author = j_author.getText();
                float price = Float.parseFloat(j_price.getText());
                int number = Integer.parseInt(j_number.getText());
                String firstlevel = j_firstlevel.getText();
                String secondlevel = j_secondlevel.getText();

                //��ȡ������Ϣ���з�װ
                Book book = new Book(bid,name,author,price,number,new Category(firstlevel, secondlevel));
                BookServiceImpl bService = new BookServiceImpl();
                String info = bService.addBook(book);
                JOptionPane.showMessageDialog(b_add, info);
                new Administrator();
            }

        });

        b_reset = new JButton("����");
        b_reset.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                j_bid.setText("");
                j_name.setText("");
                j_author.setText("");
                j_price.setText("");
                j_number.setText("");
                j_firstlevel.setText("");
                j_secondlevel .setText("");
            }
        });

        setLayout(null);

        add(l_bid);
        add(l_name);
        add(l_author);
        add(l_price);
        add(l_number);
        add(l_firstlevel);
        add(l_secondlevel);

        add(j_bid);
        add(j_name);
        add(j_author);
        add(j_price);
        add(j_number);
        add(j_firstlevel);
        add(j_secondlevel);

        add(b_add);
        add(b_reset);

        l_bid.setBounds(50, 10, 150, 30);
        j_bid.setBounds(150, 10, 150, 30);
        l_name.setBounds(50, 50, 50, 30);
        j_name.setBounds(150, 50, 150, 30);
        l_author.setBounds(50, 100, 150, 30);
        j_author.setBounds(150, 100, 150, 30);
        l_price.setBounds(50, 150, 150, 30);
        j_price.setBounds(150, 150, 150, 30);
        l_number.setBounds(50, 200, 150, 30);
        j_number.setBounds(150, 200, 150, 30);
        l_firstlevel.setBounds(50, 250, 150, 30);
        j_firstlevel.setBounds(150, 250, 150, 30);
        l_secondlevel.setBounds(50, 300, 150, 30);
        j_secondlevel.setBounds(150, 300, 150, 30);
        b_add.setBounds(50, 350, 100, 30);
        b_reset.setBounds(175,350,100,30);

    }
    public static void main(String[] args) {
        new AddBook();
    }

}

